package com.arcticcoding.pc;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.arcticcoding.dto.ImageFrame;
import com.arcticcoding.dto.Location;
import com.arcticcoding.telemetrie.TelemetrieClientListener;
import com.arcticcoding.telemetrie.TelemetrieClientPC;

public class Main {

	private JFrame frame;
	private JTextField addressField;
	private TelemetrieClientPC client;
	private ImagePainter imagePanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
		client = new TelemetrieClientPC();
		client.addClientListener(new Listener());
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		imagePanel = new ImagePainter();
		imagePanel.setBounds(26, 50, 490, 490);
		frame.getContentPane().add(imagePanel);
		
		addressField = new JTextField();
		addressField.setBounds(26, 16, 134, 28);
		frame.getContentPane().add(addressField);
		addressField.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				String address;
				
				address = addressField.getText();
				client.connect(address, 2045, 2046);
			}
		});
		btnConnect.setBounds(172, 17, 117, 29);
		frame.getContentPane().add(btnConnect);
		
		JButton btnStartStreaming = new JButton("Start Streaming");
		btnStartStreaming.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				client.sendStartStreaming();
			}
		});
		btnStartStreaming.setBounds(311, 17, 117, 29);
		frame.getContentPane().add(btnStartStreaming);
	}
	
	protected synchronized void setImage(BufferedImage image) {
		imagePanel.setImage(image);
	}
	
	private class ImagePainter extends JPanel {
		
		Image image = null;
		
		
		public void paint(Graphics g) {
			super.paint(g);
			if(image != null) {
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
			}
		}
		
		public void setImage(Image image) {
			if(image != null) {
				this.image = image;
				this.repaint();
			}
		}
	}
	
	private class Listener extends TelemetrieClientListener {

		@Override
		public void cameraImageReceived(ImageFrame frame) {
			BufferedImage image = null;
			try {
				image = ImageIO.read(new ByteArrayInputStream(frame.imageData));
			} catch(IOException e) {
				System.err.println("Bild konnte nicht gelesen werden");
				e.printStackTrace();
			}
			
			if(image != null) {
				setImage(image);
			}
		}

		@Override
		public void cameraSupportedFrameRatesReceived(List<Integer[]> frameRates) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void geoLocationReceived(Location location) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
